package com.yayan.xh.service;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.yayan.xh.bean.MatchBean;
import com.yayan.xh.bean.ProductBean;
import com.yayan.xh.dao.IMatchDao;
import com.yayan.xh.dao.IProductDao;
import com.yayan.xh.dao.MatchDao;
import com.yayan.xh.dao.ProductDao;

@Service
public class MatchService implements IMatchService{
	
	private IMatchDao matchDao;
	
	private IProductDao productDao;
	
	public MatchService() {
		this.matchDao = new MatchDao();
		this.productDao = new ProductDao();
	}

	@Override
	public List<Map<String, Object>> getMatchList(Map<String, String> map) {
		List<Map<String, Object>> list = null;
		try {
			if(null != map.get("matchId") && null != map.get("shopId") 
					&& !"0".equalsIgnoreCase(map.get("matchId")) && !"0".equalsIgnoreCase(map.get("shopId"))) {
				list = matchDao.getMatchListByMatchId(map);
			} else {
				list = matchDao.getMatchList(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public boolean saveMatch(Map<String, String> map) {
		boolean f = false;
		try {
			if(null != map.get("name") && null != map.get("address") && null != map.get("longitude") && null != map.get("latitude")
					&& !"0".equalsIgnoreCase(map.get("name")) && !"0".equalsIgnoreCase(map.get("address")) 
					&& !"0".equalsIgnoreCase(map.get("longitude")) && !"0".equalsIgnoreCase(map.get("latitude"))) {
				int id = matchDao.saveMatch(map);
				if(id > 0) {
					map.put("matchId", id+"");
					f = productDao.saveProduct(map);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return f;
	}

	@Override
	public List<MatchBean> getMatchListBack(Map<String, String> map) {
		List<MatchBean> list = null;
		try {
			list = matchDao.getMatchListBack(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public MatchBean getMatchByMatchIdBack(Map<String, String> map) {
		MatchBean bean = new MatchBean();
		try {
			bean = matchDao.getMatchByMatchIdBack(map);
			List<ProductBean> list = productDao.getProductByMatchIdBack(map);
			for (int i = 0; i < list.size(); i++) {
				ProductBean pbean = list.get(i);
				if(pbean.getP_type() == 0) {
					bean.setSproduct(pbean.getP_price());
				}
				if(pbean.getP_type() == 1) {
					bean.setEproduct(pbean.getP_price());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bean;
	}

	@Override
	public boolean modifyMatch(Map<String, String> map) {
		boolean f = false;
		try {
			if(null != map.get("name") && null != map.get("address") && null != map.get("longitude") && null != map.get("latitude")
					&& !"0".equalsIgnoreCase(map.get("name")) && !"0".equalsIgnoreCase(map.get("address")) 
					&& !"0".equalsIgnoreCase(map.get("longitude")) && !"0".equalsIgnoreCase(map.get("latitude"))) {
				f = matchDao.modifyMatch(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return f;
	}
}
